import React from 'react';
import PropTypes from 'prop-types';
import {Card, Button, Row, Col} from 'antd';
import DescriptionList from 'ant-design-pro/lib/DescriptionList';
import routes from './routes';
import {injectIntl} from './intl';
import {getBackOrderDetail} from './api';
const {Description} = DescriptionList;

export class BackOrderPanel extends React.PureComponent {
    state = {
        loading: false,
        data: {}
    }

    componentDidMount() {
        if(this.props.id)
            this.getDetailInfo();
    }

    componentWillUnmount() {
        this.isunmount = true;
    }

    getDetailInfo = () => {
        const id = this.props.id;
        this.setState({loading: true});
        getBackOrderDetail(id)
            .then(res => {
                this.setState({
                    loading: false,
                    data: res.data
                });
            });
    }
    
    render() {
        const {formatMessage} = this.props.intl;
        return (
            <div className="detail">
                <Card loading={this.state.data.loading}>
                    <DescriptionList size="large" title={<Row><Col span={6}>{formatMessage({
                        id: 'BackOrderPanel.text.title',
                        defaultMessage: 'Backorder信息'
                    })}</Col>
                    </Row>}>
                        <Description term={formatMessage({
                            id: 'backOrderPanel.text.code',
                            defaultMessage: '销售订单编号'
                        })}>{this.state.data.code}</Description>
                        <Description term={formatMessage({
                            id: 'backOrderPanel.text.partSaleOrderCode',
                            defaultMessage: '备件编号'
                        })}>{this.state.data.partSaleOrderCode}</Description>
                        <Description term={formatMessage({
                            id: 'backOrderPanel.text.oldPartCode',
                            defaultMessage: '被替换备件编号'
                        })}>{this.state.data.oldPartCode}</Description>
                        <Description term={formatMessage({
                            id: 'backOrderPanel.text.totalQuantity',
                            defaultMessage: '订购数量'
                        })}>{this.state.data.totalQuantity}</Description>
                        <Description term={formatMessage({
                            id: 'backOrderPanel.text.quantity',
                            defaultMessage: '发票数量'
                        })}>{this.state.data.quantity}</Description>
                        <Description term={formatMessage({
                            id: 'backOrderPanel.text.backorderQuantity',
                            defaultMessage: 'Backorder数量'
                        })}>{(this.state.data.totalQuantity - this.state.data.quantity).toString()}</Description>
                    </DescriptionList>
                </Card>
                <div className="page-toolbar">
                    <Button
                        type="primary"
                        disabled={this.props.loading}
                        onClick={this.props.onCancel}>{formatMessage({
                            id: 'BackOrderPanel.text.return',
                            defaultMessage: '返回'
                        })}</Button>
                </div>
            </div>
        );
    }
}

BackOrderPanel.propTypes = {
    id: PropTypes.string.isRequired,
    loading: PropTypes.bool.isRequired,
    permissions: PropTypes.array.isRequired,
    onCancel: PropTypes.func.isRequired,
    history: PropTypes.object,
    intl: PropTypes.object,
    successReturn: PropTypes.func,
};

import {connect} from 'react-redux';
import {
    tableSearch
} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const mapStateToProps = (state, props) => ({
    id: props.id,
    loading: state.getIn(['page', 'domainData', 'detail', 'isFetching']),
    permissions: getPermissions(state),
});
const mapDispatchToProps = (dispatch, props) => ({
    successReturn: () => {
        dispatch(tableSearch());
        props.history.push(routes.query.url());
    },
    onCancel: () => {
        props.history.push(routes.query.url());
    },
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(BackOrderPanel));
